package cn.xorange.commons.system.service.System.impl;

import cn.xorange.commons.core.basic.service.BaseService;
import cn.xorange.commons.data.constants.DataConstants;
import cn.xorange.commons.security.entity.SecurityUser;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.system.*;
import cn.xorange.commons.system.mapper.system.SysRoleManageOrgMapper;
import cn.xorange.commons.system.mapper.system.SysRoleManageMapper;
import cn.xorange.commons.system.mapper.system.SysUserManageRoleMapper;
import cn.xorange.commons.system.service.System.ISysRoleManageService;
import com.alibaba.fastjson2.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : yangjian
 * date : 2024/12/19
 * description :
 */
@Service
public class SysRoleManageServiceImpl extends BaseService<SysRoleManage, SysRoleManageMapper> implements ISysRoleManageService {

    @Resource
    SysRoleManageOrgMapper mOrgMapper;
    @Resource
    SysUserManageRoleMapper userMRoleMapper;

    @Override
    public List<SysRoleManage> getList(SysRoleManage sysRoleManage){
        SecurityUser user = SecurityUtils.getUser();
        sysRoleManage.setTenantId(user.getTenantId());
        return mapper.selectList(user.getIsAdmin(),user.getUserId(),sysRoleManage);
    }


    @Override
    @Transactional
    public int add(SysRoleManage sysRole){
        sysRole.setTenantId(SecurityUtils.getTenantId());
        int i= mapper.insert(sysRole);
        if(DataConstants.DATA_SCOPE_CUSTOM.equals(sysRole.getDataScope())){
            JSONArray orgIds = sysRole.getParams().getJSONArray("orgIds");
            Map<String, Object> fields = new HashMap<>(1);
            fields.put("roleId", sysRole.getRoleId());
            setRelationIds(fields, orgIds, "orgId", mOrgMapper, SysRoleManageOrg.class);
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int update(SysRoleManage sysRole){
        if(DataConstants.DATA_SCOPE_CUSTOM.equals(sysRole.getDataScope())){
            JSONArray orgIds = sysRole.getParams().getJSONArray("orgIds");
            Map<String, Object> fields = new HashMap<>(1);
            fields.put("roleId", sysRole.getRoleId());
            setRelationIds(fields, orgIds, "orgId", mOrgMapper, SysRoleManageOrg.class);
        }
        return mapper.update(sysRole);
    }

    @Override
    public List<Long> getRoleManageOrgIds(Long roleId) {
        return mOrgMapper.getManageOrgIds(roleId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int deleteById(Long roleId){
        //删除 manageRole org 对应关系
        SysRoleManageOrg mOrg = new SysRoleManageOrg();
        mOrg.setRoleId(roleId);
        mOrgMapper.del(mOrg);
        // 删除用户赋权的管理Id
        SysUserManageRole userMRole = new SysUserManageRole();
        userMRole.setManageRoleId(roleId);
        userMRoleMapper.del(userMRole);
        // 删除用户
        return mapper.delById(roleId, SysRoleManage.class);
    }
}
